package com.Jano.servlet;

import com.Jano.service.Impl.ProductServiceImpl;
import com.Jano.service.ProductService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.Set;

import static com.Jano.constant.BusinessConstant.*;

/**
 * description: ProductServlet <br>
 * date: 2022/6/10 9:52 PM <br>
 * author: zoumaoji <br>
 * version: 1.0 <br>
 */

@WebServlet(urlPatterns = "/apis/products/*")
public class ProductServlet extends AbstractBaseServlet {
    ProductService productService = new ProductServiceImpl();

    @Override
    public Object doService(String action, HttpServletRequest req, HttpServletResponse res) throws UnsupportedEncodingException {
        /**
         * 分页显示
         */
        if ("search".equals(action)){
            return productService.getProductPageInfo(paramToInt(req,"cid"),paramToInt(req,"pageNo"),DEFAULT_PAGE_SIZE);
        }

        /**
         * 根据路径中携带的商品id来查询对应的商品信息
         */
        if ("getById".equals(action)){
            return productService.getProductById(paramToInt(req,"pid"));
        }

        /**
         * 更新点击量，并且返回当前最新的点击量，每晚12点准时更新到数据库
         **/
        if ("updateClickCount".equals(action)) {
            return productService.updateClickCount(paramToInt(req, "productId"), paramToInt(req, "clickCount"));
        }

        //TODO 购物车也需要考虑未登录和登录不同的情况，如果在未登录时的购物车要考虑和登录后用户的购物车信息进行合并 ??

        /**
         * 添加商品到购物车
         * */
        if ("addItem".equals(action)) {
            try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
                 Jedis jedis = jedisPool.getResource()) {
                Boolean exists = jedis.hexists(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY), req.getParameter("pid"));
                if (exists){
                    //第二次添加了购物车中已经添加过的同一个商品，则直接把这次的数量累加到上一次相同商品的数量中去
                    jedis.hincrBy(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY), req.getParameter("pid"), Integer.parseInt(req.getParameter("count")));
                }else {
                    jedis.hset(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY), req.getParameter("pid"), req.getParameter("count"));
                }
            }
        }

        /**
         * 添加商品数量
         */
        if ("addProduct".equals(action)){
            try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
                 Jedis jedis = jedisPool.getResource()) {
                jedis.hincrBy(USER_CART_KEY+req.getSession().getAttribute(SESSION_USER_KEY),req.getParameter("productId"),paramToInt(req,"productCount"));
            }

        }

        /**
         *删除商品
         */
        if ("delProduct".equals(action)){
            try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
                 Jedis jedis = jedisPool.getResource()) {
                jedis.hdel(USER_CART_KEY+req.getSession().getAttribute(SESSION_USER_KEY),req.getParameter("productId"));
                Map<String, String> carts = jedis.hgetAll(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY));
                Long myCartsNumber = jedis.hlen(USER_CART_KEY + (String) req.getSession().getAttribute(SESSION_USER_KEY));
                return productService.myCarts(myCartsNumber,carts);
            }
        }

        /**
         * 得到当前所有购物车信息
         */

        if ("myCart".equals(action)){
            try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
                 Jedis jedis = jedisPool.getResource()) {
                Map<String, String> carts = jedis.hgetAll(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY));
                Long myCartsNumber = jedis.hlen(USER_CART_KEY + (String) req.getSession().getAttribute(SESSION_USER_KEY));
                return productService.myCarts(myCartsNumber,carts);
            }
        }

        /**
         * 提交购买，清空购物车
         */
       if ("buy".equals(action)){
           try (JedisPool jedisPool = new JedisPool(REDIS_HOST, REDIS_POST);
                Jedis jedis = jedisPool.getResource()) {
               Map<String, String> carts = jedis.hgetAll(USER_CART_KEY + req.getSession().getAttribute(SESSION_USER_KEY));
               jedis.del(USER_CART_KEY + req.getParameter("userId"));
              return productService.totalPrice(carts);
           }
       }


        return null;
    }
}
